package com.example.mypigcar.controller;

import com.example.mypigcar.common.core.SR;
import com.example.mypigcar.pojo.dto.AddAppointmentDTO;
import com.example.mypigcar.pojo.po.Appointment;
import com.example.mypigcar.pojo.vo.GetAppointmentVO;
import com.example.mypigcar.service.AppointmentService;
import com.example.mypigcar.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zkw
 * @Description 预约接口
 * @createTime 2021年07月07日 08:45:00
 */
@Api(value="预约controller",tags={"车辆预约接口"})
@RequestMapping("appointment")
@RestController
public class AppointmentController {
    @Resource
    AppointmentService appointmentService;

    @ApiOperation("添加预约单")
    @PostMapping("")
    public SR addAppointment(@RequestBody AddAppointmentDTO appointmentDTO,HttpServletRequest request){
        String usernameByToken = JWTUtils.getUserIdByToken(request);
        boolean result = false;
        try {
            result = appointmentService.insertAppointment(appointmentDTO, Integer.parseInt(usernameByToken));
        } catch (Exception e) {
            e.printStackTrace();
            return SR.error().message(e.getMessage());
        }
        return result?SR.ok():SR.error();
    }

    @ApiOperation("修改预约单状态")
    @PutMapping("")
    public SR updateAppointment(Integer appointmentId,Integer status, HttpServletRequest request){
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        Appointment appointment = new Appointment();
        appointment.setAppointmentId(appointmentId);
        appointment.setStatus(status);
        appointment.setUserId(Integer.parseInt(userIdByToken));
        System.out.println(appointment);
        boolean result = false;
        try {
            result = appointmentService.updateByUserIdAndAppointmentId(appointment);
        } catch (Exception e) {
            e.printStackTrace();
            return SR.error().message(e.getMessage());
        }
        return result?SR.ok():SR.error();
    }

    @ApiOperation("查看用户全部预约单")
    @GetMapping("")
    public SR getUserAppointment(HttpServletRequest request){
        String userIdByToken = JWTUtils.getUserIdByToken(request);
        List<Appointment> userAppointment = appointmentService.getUserAppointment(Integer.parseInt(userIdByToken));
        List<GetAppointmentVO> list = new ArrayList<>();
        for (Appointment appointment : userAppointment) {
            GetAppointmentVO getAppointmentVO =new GetAppointmentVO();
            BeanUtils.copyProperties(appointment,getAppointmentVO);
            getAppointmentVO.setCreateTime(appointment.getCreateTime().getTime());
            getAppointmentVO.setEndTime(appointment.getEndTime().getTime());
            getAppointmentVO.setStartTime(appointment.getEndTime().getTime());
            list.add(getAppointmentVO);
        }
        Collections.reverse(list);
        return SR.ok().data(list);
    }

}
